package org.caro.actions.listener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;

import org.caro.actions.ContestantAction;
import org.caro.gui.ActionZonePanel;
import org.caro.gui.UtilityZonePanel;
import org.caro.utils.Constants;
import org.caro.utils.Position;

public class LanModeExecute implements ActionListener {
	private ActionZonePanel actionZone;
	private UtilityZonePanel utilityZone;
	private ContestantAction contestant;

	public LanModeExecute(ActionZonePanel actionZone,
			UtilityZonePanel utilityZone, ContestantAction contestant) {
		this.actionZone = actionZone;
		this.utilityZone = utilityZone;
		this.contestant = contestant;
		this.contestant.getPlayRecv().doAction(this.actionZone,this.utilityZone);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		for (int i = 0; i < Constants.MAX_ROWS; i++) {
			for (int j = 0; j < Constants.MAX_COLUMNS; j++) {
				if (e.getSource() == actionZone.getButton()[i][j]
						&& actionZone.getMapButton()[i][j] == Constants.NULL
						&& ContestantAction.turn) {
					if (contestant.getmode() == Constants.HOST) {
						actionZone.getButton()[i][j].setIcon(new ImageIcon(Constants.IMAGE_PLAYER_1));
						actionZone.getMapButton()[i][j] = Constants.PLAYER1;
					} else {
						actionZone.getButton()[i][j].setIcon(new ImageIcon(Constants.IMAGE_PLAYER_2));
						actionZone.getMapButton()[i][j] = Constants.PLAYER2;
					}
					contestant.getPlaySend().doAction(new Position(i, j));
					ContestantAction.turn  = false;
//					if (CheckWinner.winner(actionZone.getMapButton(), i, j, Constants.PLAYER1) == Constants.PLAYER1) {
//						JOptionPane.showMessageDialog(null, "win");
//					}
				}
			}
		}

	}
}
